# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Sergej Pupykin <pupykin.s+arch@gmail.com>
# Contributor: robb_force <robb_force@holybuffalo.net>
# Contributor: JJDaNiMoTh <jjdanimoth@gmail.com>

pkgbase=mame
pkgname=(mame mame-tools)
pkgver=0.254
pkgrel=1
pkgdesc='Port of the popular Multiple Arcade Machine Emulator using SDL with OpenGL support'
url='https://mamedev.org/'
license=(GPL2)
arch=(loong64 x86_64)
makedepends=(nasm python asio rapidjson glm libxinerama sdl2_ttf qt5-base lua libutf8proc pugixml portmidi portaudio flac libpulse)
source=(https://github.com/mamedev/mame/archive/mame${pkgver/./}.tar.gz
        mame.sh mame.desktop mame.svg)
sha256sums=('07a5e2541e75c3511fbad00ad50d7dbd50119a7b054e393d4c64df672d8211a6'
            'ee1c59bafc5e5441e99fa4c58108a3e18048e60672f34de865c8a5a976094dba'
            '0e5e93f84673c2d17a1bf7a6b256cfa54ef89a086a828df19bc054c12776d821'
            '17c442c933d764175e4ce1de50a80c0c2ddd5d733caf09c3cd5e6ba697ac43f4')
options=(!debug !lto) # debug build fails

prepare() {
  cd mame-mame${pkgver/./}

# Use system libraries
  sed -e 's|\# USE_SYSTEM_LIB|USE_SYSTEM_LIB|g' -i makefile
# Use C++ LUA
  sed -e 's|ext_lib("lua")|ext_lib("lua++")|' -i scripts/src/main.lua -i scripts/src/3rdparty.lua
}

build() {
  cd mame-mame${pkgver/./}
  export CXXFLAGS+="  -Wp,-U_GLIBCXX_ASSERTIONS" # FS#73202
  make \
    VERBOSE=1 \
    NOWERROR=1 \
    OPTIMIZE=2 \
    TOOLS=1 \
    ARCHOPTS=-flifetime-dse=1
}

package_mame() {
  depends=(sdl2_ttf qt5-base lua libutf8proc pugixml portmidi portaudio flac libpulse hicolor-icon-theme)
  conflicts=(sdlmame)
  replaces=(sdlmame) 

  cd mame-mame${pkgver/./}

  # Install the mame script
  install -Dm755 "$srcdir"/$pkgname.sh "$pkgdir"/usr/bin/$pkgname

  # Install the binaries
  install -Dm755 mame -t "$pkgdir"/usr/lib/mame

  # Install the extra bits
  install -Dm644 src/osd/modules/opengl/shader/glsl*.*h -t "$pkgdir"/usr/lib/$pkgname/shader/
  cp -ar {artwork,bgfx,plugins,language,ctrlr,keymaps,hash} "$pkgdir"/usr/lib/$pkgname/

  # Include the license
  install -Dm644 docs/LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

  # FS#28203
  sed -i 's|KEYCODE_2_PAD|KEYCODE_2PAD|' "$pkgdir"/usr/lib/mame/ctrlr/*.cfg
  sed -i 's|KEYCODE_4_PAD|KEYCODE_4PAD|' "$pkgdir"/usr/lib/mame/ctrlr/*.cfg
  sed -i 's|KEYCODE_6_PAD|KEYCODE_6PAD|' "$pkgdir"/usr/lib/mame/ctrlr/*.cfg
  sed -i 's|KEYCODE_8_PAD|KEYCODE_8PAD|' "$pkgdir"/usr/lib/mame/ctrlr/*.cfg

  # documentation
  install -dm0755 "$pkgdir"/usr/share/doc
  cp -a docs "$pkgdir"/usr/share/doc/$pkgname
  rm -r "$pkgdir"/usr/share/doc/$pkgname/man
  install -Dm644 docs/man/*.6* -t "$pkgdir"/usr/share/man/man6/

  # install desktop file and icon
  install -Dm644 "$srcdir"/mame.desktop -t "$pkgdir"/usr/share/applications
  install -Dm644 "$srcdir"/mame.svg -t "$pkgdir"/usr/share/icons/hicolor/scalable/apps
}

package_mame-tools() {
  pkgdesc='Port of the popular Multiple Arcade Machine Emulator using SDL with OpenGL support (tools)'
  depends=(sdl2 libutf8proc flac)

  cd mame-mame${pkgver/./}
  for _i in castool chdman floptool imgtool jedutil ldresample ldverify nltool nlwav pngcmp regrep romcmp \
            split srcclean testkeys unidasm; do
    install -Dm755 $_i -t "$pkgdir"/usr/bin
  done
  mv "$pkgdir"/usr/bin/{,mame-}split # Fix conflicts

  install -Dm644 docs/man/*.1* -t "$pkgdir"/usr/share/man/man1/
}
